package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll() {
        String sql = "SELECT p.*,c.name cuisineName FROM product p ,cuisine  c where p.cuisine_id=c.id";

        return setList(sql);
    }

    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product " +
                "(name,normal_price,price," +
                "content,is_show,is_recommend," +
                "img_href,cuisine_id," +
                "create_time,update_time) " +
                "values('" + productModel.getName() + "'," + productModel.getNormalPrice() + "," + productModel.getPrice() + "," +
                "'" + productModel.getContent() + "'," + productModel.getIsShow() + "," + productModel.getIsRecommend() + "," +
                "'" + productModel.getImgHref() + "'," + productModel.getCuisineId() + ",now(),now())";
        return JDBC.update(sql);
    }

    @Override
    public ProductModel findById(int id) {
        String sql = "SELECT p.*,c.name cuisineName " +
                "FROM product p ,cuisine  c " +
                "where p.cuisine_id=c.id and p.id=" + id;
        return setList(sql).get(0);
    }

    @Override
    public Integer update(ProductModel productModel) {
        String sql = "update product set ";
        if (!productModel.getImgHref().equals("")) {
            sql += " imgHref='" + productModel.getImgHref() + "',";
        }
        sql += "createTime=now()";
        return JDBC.update(sql);
    }

    @Override
    public List<ProductModel> findForHome() {
//        String sql = "select * from product where is_recommend=1 order by id desc limit 2";
//        return setList(sql);
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.ProductDao.findForHome");
    }

    private List<ProductModel> setList(String sql) {
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setCuisineId(resultSet.getInt("cuisine_id"));
                model.setCuisineName(resultSet.getString("cuisineName"));
                model.setPrice(resultSet.getDouble("price"));
                model.setNormalPrice(resultSet.getDouble("normal_price"));
                model.setContent(resultSet.getString("content"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCreateTime(resultSet.getString("create_time"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
